<?php

require_once("View/View/PageView.php");
require_once("Common/Model/commonThings.php");
require_once("MyWorkouts/Model/WorkoutModel.php");
require_once("MyWorkouts/View/MyWorkoutsView.php");
require_once("login/src/Model/LoginValidation.php");

class MyWorkoutsController
{
	/**
	*@var PageView
	**/
	private $pageView;
	/**
	*@var MyWorkoutsView
	**/
	private $myWorkoutsView;
	/**
	*@var MyWorkoutsModel
	**/
	private $myWorkoutsModel;
	/**
	*@var Loginvalidation
	**/
	private $loginvalidation;


	public function __construct()
	{
		$this->pageView = new PageView();
		$this->myWorkoutsModel = new WorkoutModel();
		$this->myWorkoutsView = new MyWorkoutsView();
		$this->loginvalidation = new LoginValidation();
	}

	/**
	*@return string
	**/
	public function getPage()
	{
		$this->myWorkoutsModel->resetMessages();
		if($this->myWorkoutsView->isItAdded())
		{ 
			$this->addNewWorkout();
		}
		elseif($this->myWorkoutsView->isItRemove())
		{	
			$workoutId = $this->myWorkoutsView->getIdToRemove();
			$this->myWorkoutsModel->ValidateWorkoutIdAndDelete($workoutId);
			header("Location:MyWorkouts.php");
		}

		$html = $this->myWorkoutsView->getMyWorkoutsPage();
		$html = $this->pageView->getHtmlPage("Framsteg - Träningsdagboken", "", $html);
		return $html;
	}


	private function addNewWorkout()
	{ 
		if(commonThings::checkIfPost()) 
		{ 
			$name = $this->myWorkoutsView->getNewWorkoutName();
			$description = $this->myWorkoutsView->getNewWorkoutDescription();
			if($this->myWorkoutsModel->validateWorkout($name, $description))
			{	
				$reps = $this->myWorkoutsView->getExerciseReps();  // arrayer.
				$sets = $this->myWorkoutsView->getExerciseSets();
				$order = $this->myWorkoutsView->getExerciseOrder();

				$exercises = $this->myWorkoutsView->getChosenExercises();
				$this->myWorkoutsModel->CreateAndSaveWorkout($name, $description, $exercises, 
															 $reps, $sets, $order);
			}
		}
	}

	/**
	*@return bool
	**/
	public function areWeLoggedIn()
	{
		if(!$this->loginvalidation->CheckIfWereLoggedIn())
		{
			header("Location:index.php");
			return false;
		}
		return true;
	}

}